﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

using ZedGraph;

namespace UCTT.NetFx
{
    public partial class Form3 : Form
    {
        #region    字段 start
        private readonly Color[] colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Purple, Color.Cyan };
        private List<int> usedIndex = new List<int>();
        private Dictionary<Color, LineItem> curveDic = new Dictionary<Color, LineItem>();
        #endregion 字段 end

        public Form3()
        {
            InitializeComponent();
            InitCurveChart();
        }

        private void Form3_Load(object sender, System.EventArgs e)
        {

        }

        #region    私有方法 start
        private void InitCurveChart()
        {
            var gp = zgc1.GraphPane;

            //List<ushort> lblLst = new List<ushort>();
            //for (ushort i = 95; i <= 805; i += 5)
            //{
            //    lblLst.Add(i);
            //}
            //for (ushort i = 800; i >= 95; i -= 5)
            //{
            //    lblLst.Add(i);
            //}

            // Make up some data arrays based on the Sine function
            double x, y;
            PointPairList list = new PointPairList();
            //for (int i = 0; i < 37; i++)
            //{
            //    x = ((double)i - 18.0) / 5.0;
            //    y = x * x + 1.0;
            //    list.Add(x, y);
            //}
            //for (ushort i = 95; i <= 805; i += 5)
            //{
            //    x = (double)i;
            //    y = x % 500;
            //    list.Add(x, y);
            //}
            //for (ushort i = 800; i >= 95; i -= 5)
            //{
            //    x = (double)i;
            //    y = x % 500;
            //    list.Add(x, y);
            //}
            bool isDown = false;
            for (ushort i = 95; ;)
            {
                x = (double)i;
                y = x % 500;
                list.Add(x, y);
                if (i <= 805 && !isDown)
                {
                    if (i == 805)
                    {
                        isDown = true;
                    }
                    i += 5;
                }
                else
                {
                    if (i == 95)
                    {
                        break;
                    }
                    i -= 5;
                }
            }

            // Generate a red curve with diamond
            // symbols, and "Porsche" in the legend
            LineItem myCurve = gp.AddCurve("Parabola", list, Color.Cyan, SymbolType.None);
            //gp.YAxis.Cross = 805.0;
            gp.Chart.Border.IsVisible = false;
            gp.XAxis.Type = AxisType.LinearAsOrdinal;
            gp.XAxis.MajorGrid.IsVisible = true;
            //gp.XAxis.MinorGrid.IsVisible = true;
            gp.XAxis.Title.IsVisible = false;
            //gp.YAxis.MajorGrid.IsVisible = true;
            gp.YAxis.MinorGrid.IsVisible = true;
            gp.YAxis.Title.IsVisible = false;
            gp.XAxis.Scale.MajorStep = 5.0;// (gp.XAxis.Scale.Max - gp.XAxis.Scale.Min) / 10.0;    //刻度间隔大小
            gp.XAxis.Scale.MinorStep = 1.0;// (gp.XAxis.Scale.Max - gp.XAxis.Scale.Min) / 50.0;

            //gp.XAxis.Scale.TextLabels = lblLst.ToArray();
            //gp.XAxis.CrossAuto = false;//容许x轴的自动放大或缩小

            gp.XAxis.MajorTic.IsOpposite = true;
            gp.XAxis.MinorTic.IsOpposite = true;
            gp.YAxis.MajorTic.IsOpposite = true;
            gp.YAxis.MinorTic.IsOpposite = true;
            gp.YAxis.Scale.IsSkipFirstLabel = true;
            //gp.XAxis.ScaleFormatEvent += XAxis_ScaleFormatEvent;


            gp.AxisChange();
            //gp.XAxis.Type = AxisType.Linear;
        }

        private string XAxis_ScaleFormatEvent(GraphPane pane, Axis axis, double val, int index)
        {
            Debug.WriteLine($"val={val}");
            return val.ToString();
        }
        #endregion 私有方法 end

        #region    事件处理 start

        private void btn_AddCurve_Click(object sender, System.EventArgs e)
        {
            GraphPane gp = zgc1.GraphPane;
            Random random = new Random();

            int index;
            Color color;
            while (true)
            {
                index = random.Next(0, 6);
                color = colors[index];
                if (usedIndex.Contains(index))
                {
                    LineItem lineItem = curveDic[color];
                    if (gp.CurveList.Remove(lineItem))
                    {
                        usedIndex.Remove(index);
                        gp.AxisChange();
                        zgc1.Invalidate();
                        continue;
                    }
                }
                else
                {
                    usedIndex.Add(index);
                    break;
                }
            }

            double x, y;
            PointPairList list = new PointPairList();
            bool isDown = false;
            for (ushort i = 95; ;)
            {
                x = (double)i + index * 10;
                y = x % 500 - index * 10;
                list.Add(x, y);
                if (i <= 805 && !isDown)
                {
                    if (i == 805)
                    {
                        isDown = true;
                    }
                    i += 5;
                }
                else
                {
                    if (i == 95)
                    {
                        break;
                    }
                    i -= 5;
                }
            }

            LineItem curve = gp.AddCurve($"Random {index}", list, color, SymbolType.None);
            curveDic.Add(color, curve);
            gp.AxisChange();
            zgc1.Invalidate();
        }
        #endregion 事件处理 end
    }
}